﻿/*
 * Rappelz: Endless Odyssey - The first free open source Rappelz server emulator
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rappelz.GameServer.Network;
using System.Data.Common;
using System.Data;
using Rappelz.GameServer.Network.GameClient;

namespace Rappelz.GameServer.Database
{
    /// <summary>
    /// DBLoader_Dungeon
    /// </summary>
    public class DBLoader_Dungeon : IDBProc
    {
        GameDBManager db;
        public BaseObject m_CBObject = null;

        public DBLoader_Dungeon(GameDBManager dbManager)
        {
            db = dbManager;
        }

        public void SetCB(BaseObject obj)
        {
            m_CBObject = obj;
        }

        public void RunCB()
        {
            if (m_CBObject != null)
                m_CBObject.onEndQuery();
        }

        public bool onProcess()
        {
            DbConnection conn = db.CreateConnection();
            if (!LoadDungeonData(conn))
                return false;

            if (!LoadTacticalPositionData(conn))
                return false;
            return true;
        }

        private bool LoadDungeonData(DbConnection conn)
        {
            DbCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from DungeonResource";
            cmd.CommandType = CommandType.Text;

            conn.Open();
            int count = 0;
            try
            {
                DbDataReaderTypeCastHelper dr = new DbDataReaderTypeCastHelper(cmd.ExecuteReader());
                while (dr.Read())
                {
                    int off = 0;

                    int id = dr.GetInt32(off++);
                    int local_flag = dr.GetInt32(off++);
                    int dungeon_level = dr.GetInt32(off++);
                    int raid_start_pos_x = dr.GetInt32(off++);
                    int raid_start_pos_y = dr.GetInt32(off++);
                    int siege_start_pos_x = dr.GetInt32(off++);
                    int siege_start_pos_y = dr.GetInt32(off++);
                    int defence_pos_x = dr.GetInt32(off++);
                    int defence_pos_y = dr.GetInt32(off++);
                    int connector_id = dr.GetInt32(off++);
                    int connector_pos_x = dr.GetInt32(off++);
                    int connector_pos_y = dr.GetInt32(off++);
                    int connector_direction = dr.GetInt32(off++);
                    int core_id = dr.GetInt32(off++);
                    int core_pos_x = dr.GetInt32(off++);
                    int core_pos_y = dr.GetInt32(off++);
                    float core_offset_x = (float)dr.GetDecimal(off++);
                    float core_offset_y = (float)dr.GetDecimal(off++);
                    float core_offset_z = (float)dr.GetDecimal(off++);
                    float core_around_x = (float)dr.GetDecimal(off++);
                    float core_around_y = (float)dr.GetDecimal(off++);
                    float core_around_z = (float)dr.GetDecimal(off++);
                    float scale_x = (float)dr.GetDecimal(off++);
                    float scale_y = (float)dr.GetDecimal(off++);
                    float scale_z = (float)dr.GetDecimal(off++);
                    byte is_height_locked = (byte) dr.GetByte(off++);
                    float lock_height = (float)dr.GetDecimal(off++);
                    int boss01_id = dr.GetInt32(off++);
                    int boss02_id = dr.GetInt32(off++);
                    int raid_opening_time = dr.GetInt32(off++);
                    int raid_closing_time = dr.GetInt32(off++);
                    int siege_opening_time = dr.GetInt32(off++);
                    int siege_closing_time = dr.GetInt32(off++);
                    int seamless_x = dr.GetInt32(off++);
                    int seamless_y = dr.GetInt32(off++);
                    int num_party_guild = dr.GetInt32(off++);
                    int num_party_mercenary = dr.GetInt32(off++);
                    int num_party_raid = dr.GetInt32(off++);

                    X2D.Boxf box = new X2D.Boxf();
                    box.begin = new X2D.Pointf((seamless_x) * GameContent.g_fMapLength, (seamless_y) * GameContent.g_fMapLength);
                    box.end = new X2D.Pointf((seamless_x+1) * GameContent.g_fMapLength, (seamless_y+1) * GameContent.g_fMapLength);

                    DbConnection conn2 = GameDBManager.Instance.CreateConnection();
                    DbCommand cmd2 = conn2.CreateCommand();

                    cmd2.CommandText = "smp_read_dungeon";
                    cmd2.CommandType = CommandType.StoredProcedure;

                    DbParameter IN_ACCOUNT_ID = GameDBManager.Instance.CreateInParameter(cmd2, "IN_DUNGEON_ID", DbType.Int32, id);

                    conn2.Open();

                    DbDataReaderTypeCastHelper dr2 = new DbDataReaderTypeCastHelper(cmd2.ExecuteReader());
                    while (dr2.Read())
                    {
                        off = 0;

                        int owner_guild_id = dr2.GetInt32(off++);
                        int raid_guild_id = dr2.GetInt32(off++);
                        int best_raid_time = dr2.GetInt32(off++);
                        int last_dungeon_siege_finish_time = dr2.GetInt32(off++);
                        int last_dungeon_raid_wrap_up_time = dr2.GetInt32(off++);
                        int tax_rate = dr2.GetInt32(off++);

                        DungeonManager.Instance.RegisterDungeonInfo(id,dungeon_level,(float)raid_start_pos_x,(float)raid_start_pos_y,
                            (float)siege_start_pos_x,(float)siege_start_pos_y,(float)defence_pos_x,(float)defence_pos_y,connector_id,
                            (float)connector_pos_x,(float)connector_pos_y, core_id,(float)core_pos_x,(float)core_pos_y,(float)core_offset_z,
                            (float)core_around_x,(float)core_around_y,(float)core_around_z,(float)scale_x,(float)scale_y,(float)scale_z,
                            is_height_locked != 0, (float)lock_height,boss01_id,boss02_id,raid_opening_time,raid_closing_time,siege_opening_time,
                            siege_closing_time,box,owner_guild_id,raid_guild_id,best_raid_time,last_dungeon_siege_finish_time,last_dungeon_raid_wrap_up_time,
                            tax_rate,num_party_guild,num_party_raid);


                    }

                    conn2.Close();

//                     X2D.Boxf box = new X2D.Boxf();
//                     box.SetLeft(left);
//                     box.SetTop(top);
//                     box.SetRight(right);
//                     box.SetBottom(bottom);
// 
//                     ChannelManager.RegisterChannelInfo(id, box, channel_type);
                    count++;
                }
            }
            catch (System.Data.SqlClient.SqlException e)
            {
                Globals.Log.Warning("Can't load DungeonResource database, Sql string: {0}", e.Message);
            }
            catch (Exception ex)
            {
                Globals.Log.Error("DBLoader_Dungeon.LoadDungeonData Exception: {0}", ex);
            }
            conn.Close();
            Globals.Log.Info("Total {0} DungeonResource loaded.", count);
            return true;
        }

        private bool LoadTacticalPositionData(DbConnection conn)
        {
            DbCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from TacticalPositionResource";
            cmd.CommandType = CommandType.Text;

            conn.Open();
            int count = 0;
            try
            {
                DbDataReaderTypeCastHelper dr = new DbDataReaderTypeCastHelper(cmd.ExecuteReader());
                while (dr.Read())
                {
                    int off = 0;

                    int dungeon_id = dr.GetInt32(off++);
                    int id = dr.GetInt32(off++);
                    int x = dr.GetInt32(off++);
                    int y = dr.GetInt32(off++);
                    int prop_id = dr.GetInt32(off++);

//                    ChannelManager.RegisterUserLimitChannelInfo(id, proper_user, max_channel_num);
                    count++;
                }
            }
            catch (System.Data.SqlClient.SqlException e)
            {
                Globals.Log.Warning("Can't load TacticalPositionResource database, Sql string: {0}", e.Message);
            }
            catch (Exception ex)
            {
                Globals.Log.Error("DBLoader_Dungeon.LoadTacticalPositionData Exception: {0}", ex);
            }
            conn.Close();
            Globals.Log.Info("Total {0} TacticalPositionResource loaded.", count);
            return true;

        }


    }

}
